//Chen0125 Help OP
//LiteLoaderScript Dev Helper
/// <reference path="c:\Users\Administrator\.vscode\extensions/dts/llaids/src/index.d.ts"/> 
var ver = "2.0.0"
ll.registerPlugin("X_T","Help OP",[0,0,1]);

var config = {
    "lang":{
        "zh_CN":{
            "un":"此模块未启用",
            "xr":[
                "xr",
                "重载插件"
            ],
            "xkick":[
                "xkick",
                "踢出玩家"
            ],
            "menu":[
                "open",
                "打开表单",
                "此文件不存在",
                "您的权限不足",
                "menu",
                "更改表单"
            ],
            "command":[
                "您的权限不足",
                "rcmd",
                "重载command"
            ]
        },
        "en":{
            "un":"not true",
            "xr":[
                "xr",
                "Reload X_T"
            ],
            "xkick":[
                "xkick",
                "Kick players"
            ],
            "menu":[
                "open",
                "Open menu",
                "This file is not ture",
                "Your level can't open it",
                "menu",
                "change menu"
            ],
            "command":[
                "Your level can't use it",
                "rcmd",
                "reload command"
            ]
        }
    },
    "Raw":{
       "path":"./plugins/X_T/",
       "lang":"zh_CN",
       "menu_path":"./plugins/X_T/menu/"
    },
    "config":{
        "autoUpdate":false,
        "xkick":true,
        "menu":true,
        "command":true
    },
    "autoUpdate":{
        "time":86400
    },
    "command":[
        {
            "name":"main",
            "text":"打开main表单",
            "level":1,
            "type":0,
            "alias":"xm",
            "command":["open {x_player.realName} main"]
        }
    ]
};
//raw
function Raw() {
    if (!File.exists("./plugins/X_T/Raw.json")) {
        File.writeTo("./plugins/X_T/Raw.json", JSON.stringify(config.Raw, null, "\t"));
    };
    return JSON.parse(file.readFrom("./plugins/X_T/Raw.json"));
};

function rc() {
    if (!File.exists(Raw().path + "lang/" + Raw().lang + ".json")) {
        File.writeTo(Raw().path + "lang/" + Raw().lang + ".json", JSON.stringify(config.lang[Raw().lang], null, "\t"));
    };
    return JSON.parse(file.readFrom(Raw().path + "lang/" + Raw().lang + ".json"));
};

function re(pl) {
    if (pl == undefined) {
        return rc();
    }
    if (!File.exists(Raw().path + "lang/" + pl.langCode + ".json")) {
        if (config.lang[pl.langCode] == undefined) {
            if (!File.exists(Raw().path + "lang/en.json")) {
                File.writeTo(Raw().path + "lang/en.json", JSON.stringify(config.lang["en"], null, "\t"));
            }
            return JSON.parse(file.readFrom(Raw().path + "lang/en.json"));
        } else {
            File.writeTo(Raw().path + "lang/" + pl.langCode + ".json", JSON.stringify(config.lang[pl.langCode], null, "\t"));
        };
    };
    return JSON.parse(file.readFrom(Raw().path + "lang/" + pl.langCode + ".json"));
};

function co() {
    if (!File.exists(Raw().path + "config.json")) {
        File.writeTo(Raw().path + "config.json", JSON.stringify(config.config, null, "\t"));
    };
    return JSON.parse(file.readFrom(Raw().path + "config.json"))
}
//ob
var x_server = {
    OnlinePlayers() {
        return mc.getOnlinePlayers();
    },
    BDSVersion() {
        return mc.getBDSVersion();
    },
    ServerProtocolVersion() {
        return mc.getServerProtocolVersion();
    }
}

var x_player = {
    name(pl) {
        return pl.name;
    },
    realName(pl) {
        return pl.realName;
    },
    posX(pl) {
        return parseInt(pl.pos.x);
    },
    posY(pl) {
        return parseInt(pl.pos.y);
    },
    posZ(pl) {
        return parseInt(pl.pos.z);
    },
    pos(pl) {
        return `x:${parseInt(plr.pos.x)}|y:${parseInt(pl.pos.y)}|z:${parseInt(pl.pos.z)}`;
    },
    xuid(pl) {
        return pl.xuid;
    },
    uuid(pl) {
        return pl.uuid;
    },
    gameMode(pl) {
        return pl.gameMode == 0 ? "生存" : pl.gameMode == 1 ? "创造" : pl.gameMode == 2 ? "冒险" : "旁观";
    },
    maxHealth(pl) {
        return pl.maxHealth;
    },
    health(pl) {
        return pl.health;
    },
    inAir(pl) {
        return pl.inAir ? "是" : "否";
    },
    inWater(pl) {
        return pl.inWater ? "是" : "否";
    },
    sneaking(pl) {
        return pl.sneaking ? "是" : "否";
    },
    speed(pl) {
        return pl.speed.toFixed(2);
    },
    direction(pl) {
        let directions = pl.direction.toFacing();
        return directions == 0 ? "北" : directions == 1 ? "东" : directions == 2 ? "南" : "西";
    },
    dim(pl) {
        return pl.pos.dim;
    },
    realName(pl) {
        return pl.realName;
    },
    uniqueId(pl) {
        return pl.uniqueId;
    },
    isOP(pl) {
        return pl.isOP() ? "是" : "否";
    },
    Hand(pl) {
        return pl.getHand().name;
    },
    HandId(pl) {
        return pl.getHand().id;
    },
    HandType(pl) {
        return pl.getHand().type;
    },
    HandCount(pl) {
        return pl.getHand().count;
    },
}

function ob(data, pl) {
    let rd = /{(\w+)\.(\w+)}/g.exec(data);
    if (rd == undefined) return data;
    rd.forEach((v, k) => {
        data = data.replace(/{(\w+)\.(\w+)}/g, (...ages) => this.om(pl, ages[1], ages[2]));
    });
    return data;
}

function om(pl, type, value) {
    let data;
    switch (type) {
        case "x_server":
            data = x_server[value];
            break;
        case "x_player":
            data = x_player[value](pl);
            break;
    }
    return data;
}
//au
function au() {
    xu();
    if (!File.exists(Raw().path + "autoUpdate/autoUpdate.json")) {
        File.writeTo(Raw().path + "autoUpdate/autoUpdate.json", JSON.stringify(config.autoUpdate, null, "\t"));
    };
    let au = JSON.parse(file.readFrom(Raw().path + "autoUpdate/autoUpdate.json"))
    setTimeou(au(), au.time * 1000)
}

function xu() {
    network.httpGet("https://gitee.com/chen-0125/X_P/raw/master/ver/X_Tver.txt", (status, nver) => {
        if (status == 200 || nver != ver) {
            network.httpGet("https://gitee.com/chen-0125/X_P/raw/master/plugins/X_T.js", (status, njs) => {
                if (status == 200) {
                    File.writeTo(".\\plugins\\X_T.js", njs);
                    setTimeout(xr(),500)
                }
            })
        }
    })
}
//xr
function xr() {
    let n = File.getFilesList("./plugins/");
    for (let i = 0; i < n.length; i++) {
        if (!File.checkIsDir("./plugins/" + n[i])) {
            let fi = new File("./plugins/" + n[i], file.ReadMode);
            if (fi.readLineSync() == "//Chen0125 Help OP") {
                mc.runcmdEx("ll unload X_T");
                mc.runcmdEx("ll load ./plugins/" + n[i]);
                fi.close();
            };
        };
    };
};

function xreload() {
    let cmd = mc.newCommand(rc().xr[0], rc().xr[1], PermType.GameMasters, 0);
    cmd.overload();
    cmd.setCallback((_cmd, _ori, out, res) => {
        xr();
        return;
    });
    cmd.setup();
};
//xk
function xkick() {
    let cmd = mc.newCommand(rc().xkick[0], rc().xkick[1], PermType.GameMasters, 0);
    cmd.mandatory("player", ParamType.Player);
    cmd.optional("text", ParamType.RawText);
    cmd.overload(["player", "text"]);
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!co().xkick) {
            return out.addMessage(re(_ori.player).un);
        };
        if (res.text != undefined) {
            for(let i = 0; i < res.player.length; i++) {
                res.player[i].kick(res.text);
            };
        } else {
            for(let i = 0; i < res.player.length; i++) {
                res.player[i].kick();
            };
        };
        return;
    });
    cmd.setup();
};
//menu
function menu() {
    let cmd = mc.newCommand(rc().menu[0], rc().menu[1], PermType.Any, 0);
    cmd.mandatory("player", ParamType.Player);
    cmd.mandatory("menu", ParamType.RawText);
    cmd.overload(["player", "menu"]);
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!co().menu) {
            return out.addMessage(re(_ori.player).un)
        };
        if (!File.exists(Raw().menu_path + res.menu + ".json")) {
            return out.error(re(_ori.player).menu[2]);
        };
        let f = ob(JSON.parse(file.readFrom(Raw().menu_path + res.menu + ".json")));
        if (_ori.player != undefined && _ori.player.permLevel < f.level) {
            return out.addMessage(re(_ori.player).menu[3]);
        };
        if (f.button != undefined) {
            let m = mc.newSimpleForm();
            m.setTitle(f.title);
            m.setContent(f.content);
            for(let i = 0; i < f.button.length; i++) {
                m.addButton(f.button[i].text, f.button[i].image);
            };
            for(let i = 0; i < res.player.length; i++) {
                res.player[i].sendForm(m, (pl, id) => {
                    if (id != undefined) {
                        for(let i = 0; i < f.button[id].command.length; i++) {
                            mc.runcmd(ob(f.button[id].command[i], pl));
                        };
                    };
                })
            };
            return;
        } else {
            let m = mc.newCustomForm();
            m.setTitle(f.title);
            for(let i = 0; i < f.content.length; i++) {
                switch (f.content[i].type) {
                    case "label":
                        m.addLabel(f.content[i].text);
                        break;
                    case "input":
                        m.addInput(f.content[i].title,f.content[i].placeholder,f.content[i].default);
                        break;
                    case "switch":
                        m.addSwitch(f.content[i].title,f.content[i].default);
                        break;
                    case "dropdown":
                        m.addDropdown(f.content[i].title,f.content[i].items,f.content[i].default);
                        break;
                    case "slider":
                        m.addSlider(f.content[i].title,f.content[i].min,f.content[i].max,f.content[i].step,f.content[i].default);
                        break;
                    case "stepSlider":
                        m.addStepSlider(f.content[i].title,f.content[i].items,f.content[i].default);
                        break;
                }
            }
            for(let i = 0; i < res.player.length; i++) {
                res.player[i].sendForm(m, (pl, dt) => {
                    if (dt != undefined) {
                        for(let i = 0; i < f.command.length; i++) {
                            for(let a = 0; a < f.content.length; a++) {
                                f.command[i] = f.command[i].replace(`dt[${a}]`, dt[a]);
                            }
                            mc.runcmd(ob(f.command[i], pl));
                        };
                    };
                })
            };
        }
    });
    cmd.setup();
};

/*function wenu() {
    let cmd = mc.newCommand(rc().menu[4], rc().menu[5], PermType.GameMasters, 0);
    cmd.mandatory("menu", ParamType.RawText);
    cmd.overload("menu");
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!co().menu) {
            return out.addMessage(re(_ori).un)
        }
        let m = mc.newCustomForm()
        if (File.exists(Raw().menu_path) + res.menu + ".json") {
            let f = JSON.parse(file.readFrom(Raw().menu_path + res.menu + ".json"));
            m.setTitle(res.menu);
            m.addSwitch(re(_ori).menu[6],re(_ori).menu[7]);
            m.addInput(re(_ori).menu[8],re(_ori).menu[9],String.raw(f.level));
            m.addInput(re(_ori).menu[10],re(_ori).menu[9],String.raw(f.title));
            m.addInput(re(_ori).menu[11],re(_ori).menu[9],String.raw(f.content));
            for(let i = 0; i < f.button.length; i++) {
                m.addSwitch(re(_ori).menu[12],re(_ori).menu[13]);
                m.addInput(re(_ori).menu[11],re(_ori).menu[9],String.raw(f.button[i].text));
                m.addInput(re(_ori).menu[11],re(_ori).menu[9],String.raw(f.button[i].image));
                m.addInput(re(_ori).menu[11],re(_ori).menu[9],String.raw(f.button[i].command));
            }
        }
    });
    cmd.setup();
};*/
//cmd
function comd() {
    if (!File.exists(Raw().path + "command/command.json")) {
        File.writeTo(Raw().path + "command/command.json", JSON.stringify(config.command, null, "\t"));
    };
    let f = JSON.parse(file.readFrom(Raw().path + "command/command.json"));
    for(let i = 0; i < f.length; i++) {
        let cmd = mc.newCommand(f[i].name, f[i].text, PermType.Any, f[i].type, f[i].alias);
        cmd.overload();
        cmd.setCallback((_cmd, _ori, out, res) => {
            if (!co().command) {
                return out.addMessage(re(_ori.player).un);
            };
            if (_ori.player != undefined && _ori.player.permLevel < f[i].level) {
                return out.addMessage(re(_ori.player).command[0]);
            };
            for(let a = 0; a < f[i].command.length; a++) {
                mc.runcmd(ob(f[i].command[a], _ori.player));
            };
        });
        cmd.setup();
    }
}

function rcmd() {
    let cmd = mc.newCommand(rc().command[1], rc().command[2], PermType.GameMasters, 0);
    cmd.overload();rc()
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!co().command) {
            return out.addMessage(re(_ori.player).un);
        };
        comd();
        return;
    });
    cmd.setup();
}
//load
function loads() {
    if (co().autoUpdate) {
        au();
    };
    xreload();
    if (co().xkick) {
        xkick();
    };
    if (co().menu) {
        menu();
        //wenu();
    };
    if (co().command) {
        comd();
        rcmd();
    };
};

loads();